<?php

/*
 * This class is used for as Model for Contractor table 
 */

class Model_Contractor extends Model_Base {

    protected $_name = 'tbl_contractor';
    protected $_primaryKey = 'ctr_id';
    
    /**
     * Function to fetch contractor and login details for managing session with given Log-In ID.
     *
     * @param  array|$dataArray
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getContractorAuthData($dataArray = Array()) {
        $select = $this->select()
                ->from(array('lgn' => 'tbl_login'))
                ->setIntegrityCheck(false)
                ->join(array('ctr' => 'tbl_contractor'), "lgn.lgn_id = ctr.lgn_id")
                ->where("ctr.lgn_id = '" . $dataArray['lgn_id'] . "'")
                ->where("lgn.lgn_id = '" . $dataArray['lgn_id'] . "'");
        $data = parent::fetchRow($select);
        $object = new stdClass();
        $object->usr_id = $data->ctr_id;
        $object->ctr_id = $data->ctr_id;
        $object->lgn_id = $data->lgn_id;
        $object->lgn_email = $data->lgn_email;
        $object->lgn_password = $data->lgn_password;
        $object->lgn_type = $data->lgn_type;
        $object->name = $data->ctr_name;
        $object->role = "CONTRACTOR";
        $object->lgn_created = $data->lgn_created;
        $object->lgn_modified = $data->lgn_modified;
        $object->lgn_active = $data->lgn_active;
        return $object;
    }

    /**
     * Function to return different status of the Contractor
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getStatusArray() {
        return array(
            '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('ctr' => 'tbl_contractor'))
                ->setIntegrityCheck(false)
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = ctr.lgn_id")
                ->where('ctr_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get contractors
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchContractors($where = "", $limit = "", $offset = "0", $order = 'ctr_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('ctr' => 'tbl_contractor'))
                ->setIntegrityCheck(false)
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = ctr.lgn_id");
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total contractors
     * @return int
     */
    public function getContractorCount($where) {
        $select = $this->select()
                ->from(array('ctr' => 'tbl_contractor'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = ctr.lgn_id");
        if ($where != '') {
            $select->where($where);
        }

        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete contractors
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteContractor($contractorArray) {
        $loginModel = new Model_Login();
        $config = Zend_Registry::get('config');
        if (is_array($contractorArray)) {
            foreach ($contractorArray as $_contractor) {
                $contractorData = Array();
                $contractorData = $this->fetchEntryById($_contractor);
                if (count($contractorData) > 0) {
                    $lgn_id = $contractorData['lgn_id'];
                    #$this->delete('ctr_id = ' . (int) $_contractor);

                    //Deletion of the images
                    $folder = $config->UPLOAD_PATH . "contractor/";
                    $logo = $folder . $contractorData['ctr_image'];
                    $thumb_logo = $folder . "thumb_" . $contractorData['ctr_image'];
                    if (file_exists($logo)) {
                        @unlink($logo);
                    }
                    if (file_exists($thumb_logo)) {
                        @unlink($thumb_logo);
                    }
                    $loginModel->delete("lgn_id = '" . $lgn_id . "'");
                }
            }
        } else {
            $ctr_id = $contractorArray;
            $contractorData = $this->fetchEntryById($ctr_id);
            if (count($contractorData) > 0) {
                $lgn_id = $contractorData['lgn_id'];

                //Deletion of the images
                $folder = $config->UPLOAD_PATH . "contractor/";
                $logo = $folder . $contractorData['ctr_image'];
                $thumb_logo = $folder . "thumb_" . $contractorData['ctr_image'];
                if (file_exists($logo)) {
                    @unlink($logo);
                }
                if (file_exists($thumb_logo)) {
                    @unlink($thumb_logo);
                }
                $this->delete('ctr_id = ' . (int) $ctr_id);
                $loginModel->delete("lgn_id = '" . $lgn_id . "'");
            }
        }
    }

}
